<template>
  <div class="contianer">
    <div class="header">
      <el-input
        v-model="input"
        placeholder="请输入员工姓名"
        clearable
        @keyup.enter.native="debouncedInit"
      >
        <i
          slot="prefix"
          class="el-input__icon el-icon-search"
          @click="debouncedInit"
        ></i>
      </el-input>
      <el-button icon="el-icon-plus">添加员工</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="员工姓名"> </el-table-column>
      <el-table-column prop="username" label="账号"> </el-table-column>
      <el-table-column prop="phone" label="手机号"> </el-table-column>
      <el-table-column label="状态">
        <template v-slot="{ row }">
          {{ row.status ? "正常" : "已禁用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该账号的状态?"
            @onConfirm="handleChangeStatus(row)"
          >
            <el-link type="danger" slot="reference">{{
              row.status ? "禁用" : "启用"
            }}</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="pageInfo.page"
        :limit.sync="pageInfo.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        @pagination="handlePagination"
      />
    </div>
  </div>
</template>

<script>
import { getEmployees, changeStatus } from "@/api/employee";
import { debounce } from "lodash";
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  data() {
    return {
      input: "",
      tableData: [],
      multipleSelection: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
    };
  },
  created() {
    this.init();
  },
  methods: {
    debouncedInit: debounce(function () {
      this.init();
    }, 500),

    init() {
      getEmployees({ ...this.pageInfo, name: this.input }).then((res) => {
        const { records, total } = res.data;
        this.tableData = records;
        this.total = total;
      });
    },
    handlePagination({ page, limit }) {
      this.pageInfo.page = page;
      this.pageInfo.pageSize = limit;
      this.init();
    },
    async handleChangeStatus(row) {
      await changeStatus({ id: row.id, status: +!row.status });
      this.$message.success("改变成功");
      this.init();
    },
    beforeUnmount() {
      this.debouncedInit.cancel && this.debouncedInit.cancel();
    },
  },
};
</script>

<style lang="scss" scoped>
.contianer {
  padding: 33px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .el-input {
      width: 250px;
      i {
        cursor: pointer;
      }
    }
    .right-actions {
      display: flex;
      align-items: center;
      .el-link {
        padding: 0 16px;
      }
      .el-divider {
        margin: 0;
        height: 1em;
        background-color: #dcdfe6;
      }
      .el-button {
        margin-left: 16px;
      }
    }
  }
  .footer {
    margin-top: 22px;
    display: flex;
    justify-content: center;
  }
}
</style>





